import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestResultFrame extends JFrame {
    private String account;

    public TestResultFrame(String account, String calculatedMBTI) {
        this.account = account;
        setTitle("MBTI职业性格测试 - 测试结果");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // 获取测试结果数据
        ResultDAOImpl resultDAO = new ResultDAOImpl();
        Result result = resultDAO.findByAccount(account);
        if (result != null) {
            // 查询MBTI类型对应的详细信息
            MBTIDAOImpl mbtiDAO = new MBTIDAOImpl();
            MBTI mbti = mbtiDAO.findByType(result.getMbti());

            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.insets = new Insets(5, 5, 5, 5);
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.anchor = GridBagConstraints.WEST;

            // 显示账号信息
            JLabel accountLabel = new JLabel("账号: " + account);
            panel.add(accountLabel, gbc);

            // 显示MBTI类型
            gbc.gridy++;
            JLabel mbtiTypeLabel = new JLabel("MBTI类型: " + calculatedMBTI);
            panel.add(mbtiTypeLabel, gbc);

            // 显示MBTI特征和特征类型
            if (mbti != null) {
                gbc.gridy++;
                JLabel featureLabel = new JLabel("特征: " + mbti.getFture());
                panel.add(featureLabel, gbc);

                gbc.gridy++;
                JLabel fTypeLabel = new JLabel("特征类型: " + mbti.getFType());
                panel.add(fTypeLabel, gbc);
            }

            // 返回按钮
            gbc.gridy++;
            gbc.anchor = GridBagConstraints.CENTER;
            JButton backButton = new JButton("返回");
            backButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                    // 这里可以根据实际需求决定返回的界面，例如返回测试用户界面
                    new TestUserFrame(account).setVisible(true);
                }
            });
            panel.add(backButton, gbc);

            add(panel);
            pack(); // 自适应组件大小
            setLocationRelativeTo(null); // 将窗口显示在屏幕中央
        } else {
            JOptionPane.showMessageDialog(this, "未找到测试结果，请先进行测试。");
            dispose();
        }
    }
}